package com.caowei.weicimpress.weicimpress;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by caoweiPc on 2014/7/15.
 */
public class TaskFragment extends BaseFragment {
    private static final String TAG=TaskFragment.class.getSimpleName();
    @InjectView(R.id.list1)
    ListView list1;
    private TaskAdapter taskAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.taskfragment_layout,container,false);
        ButterKnife.inject(this, v);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        taskAdapter=new TaskAdapter(getActivity());

        taskAdapter.setmData(MyApplication.getMyApplication().getAsynTaskBeans());
        list1.setAdapter(taskAdapter);

    }

    @Override
    public void refresh() {
        taskAdapter.notifyDataSetChanged();
    }

    class TaskAdapter extends BaseAdapter {
        private List< MyApplication.AsynTaskBean> mData;
        private Context mContext;
        private LayoutInflater inflater;
        public TaskAdapter(Context mContext) {
            this.mContext = mContext;
            inflater=LayoutInflater.from(mContext);
        }

        public List< MyApplication.AsynTaskBean> getmData() {
            return mData;
        }

        public void setmData(List< MyApplication.AsynTaskBean> mData) {
            this.mData = mData;
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public  MyApplication.AsynTaskBean getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return mData.get(position).hashCode();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder=null;
            if(convertView==null){
                convertView= inflater.inflate(R.layout.taskfragment_layout_item,parent,false);
                viewHolder=new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else{
                viewHolder= (ViewHolder) convertView.getTag();
            }
            final MyApplication.AsynTaskBean bean= getItem(position);
            viewHolder.name.setText(bean.getAsyntaskName());
            final ViewHolder viewHolder1=viewHolder;
            viewHolder.cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final AsyncTask asyncTask= bean.getTask();
                    if(asyncTask!=null&&!asyncTask.getStatus().equals(AsyncTask.Status.FINISHED)){

                        View.OnClickListener yesListener=new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if(asyncTask.cancel(false)){
                                    String tip=TaskFragment.this.getActivity().
                                            getString(R.string.cancelingtasktip,bean.getAsyntaskName());
                                    viewHolder1.name.setText(tip);
                                    viewHolder1.cancel.setEnabled(false);
                                }else{
                                    throw new MyException(getActivity().getString(R.string.canceltaskerrortip,
                                            bean.getAsyntaskName()),MyException.ERROR);
                                }
                            }
                        };

                        com.caowei.weicimpress.weicimpress.widget.AlertDialog dialog= GenerateDialog.createVerifyDialog(TaskFragment.this.getActivity(),
                                TaskFragment.this.getActivity().getString(R.string.verify_title),
                                yesListener, null);
                        dialog.show();
                    }else{
                        //todo
                    }
                }
            });
            return convertView;
        }

        class ViewHolder{
            @InjectView(R.id.name)
            TextView name;
            @InjectView(R.id.cancel)
            View cancel;
            public ViewHolder(View v){
                ButterKnife.inject(this,v);
            }
        }
    }
}
